package com.bi.design.mqtt.borker;

import com.bi.design.mqtt.borker.auth.IAuthService;
import com.bi.design.mqtt.borker.message.IMessageIdService;
import com.bi.design.mqtt.borker.protocol.*;
import com.bi.design.mqtt.borker.store.session.ISessionStoreService;
import com.bi.design.mqtt.borker.store.subscribe.ISubscribeStoreService;
import com.bi.design.mqtt.borker.props.BrokerProperties;
import io.netty.channel.ChannelId;
import io.netty.channel.group.ChannelGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
public class ProtocolProcess {


    @Autowired
    private ISessionStoreService sessionStoreService;

    @Autowired
    private ISubscribeStoreService subscribeStoreService;

    @Autowired
    private IAuthService authService;

    @Autowired
    private IMessageIdService messageIdService;

    @Autowired
    private BrokerProperties brokerProperties;

    @Autowired
    private ChannelGroup channelGroup;

    @Autowired
    private Map<String, ChannelId> channelIdMap;

    private Connect connect;
    private Subscribe subscribe;
    private UnSubscribe unSubscribe;
    private Publish publish;
    private DisConnect disConnect;
    private PingReq pingReq;
    private PubRel pubRel;
    private PubAck pubAck;
    private PubRec pubRec;
    private PubComp pubComp;

    public Connect connect() {
        if (connect == null) {
            connect = new Connect(sessionStoreService, subscribeStoreService, authService, brokerProperties, channelGroup, channelIdMap);
        }
        return connect;
    }

    public Subscribe subscribe() {
        if (subscribe == null) {
            subscribe = new Subscribe(subscribeStoreService);
        }
        return subscribe;
    }

    public UnSubscribe unSubscribe() {
        if (unSubscribe == null) {
            unSubscribe = new UnSubscribe(subscribeStoreService);
        }
        return unSubscribe;
    }

    public Publish publish() {
        if (publish == null) {
            publish = new Publish(sessionStoreService, subscribeStoreService, messageIdService, channelGroup, channelIdMap);
        }
        return publish;
    }

    public DisConnect disConnect() {
        if (disConnect == null) {
            disConnect = new DisConnect(sessionStoreService, subscribeStoreService);
        }
        return disConnect;
    }

    public PingReq pingReq() {
        if (pingReq == null) {
            pingReq = new PingReq();
        }
        return pingReq;
    }

    public PubRel pubRel() {
        if (pubRel == null) {
            pubRel = new PubRel();
        }
        return pubRel;
    }

    public PubAck pubAck() {
        if (pubAck == null) {
            pubAck = new PubAck();
        }
        return pubAck;
    }

    public PubRec pubRec() {
        if (pubRec == null) {
            pubRec = new PubRec();
        }
        return pubRec;
    }

    public PubComp pubComp() {
        if (pubComp == null) {
            pubComp = new PubComp();
        }
        return pubComp;
    }
}
